package org.aynu.parkingservice.controller;

import org.aynu.parkingservice.common.ParkingResponse;
import org.aynu.parkingservice.entity.ParkingEntity;
import org.aynu.parkingservice.entity.ReserEntity;
import org.aynu.parkingservice.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static org.aynu.parkingservice.common.ParkingStatusCode.ReserStatus_active;
import static org.aynu.parkingservice.common.ParkingStatusCode.ParkingStatus_available;

/**
 * 停车控制器
 *
 * @author 小粥
 * @since 2025/03/03
 */
@RestController
@RequestMapping("/parking")
public class ParkingController {
    /** 停车服务 */
    private final ParkingService parkingService;
    @Autowired
    public ParkingController(ParkingService parkingService) {
        this.parkingService = parkingService;
    }

    /**
     * 创建 Parking
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    // 创建车位
    @RequestMapping(value = "/createParking", method = RequestMethod.POST)
    public ParkingResponse<Integer> createParking(@RequestBody Map<String, Object> map) {
        ParkingEntity parkingEntity = new ParkingEntity();
        parkingEntity.setSpotNumber(map.get("spotNumber").toString());
        parkingEntity.setStatus(map.get("status").toString());
        parkingEntity.setLocation(map.get("location").toString());
        return parkingService.createParking(parkingEntity);
    }

    /**
     * 选择 Parking by ID （按 ID 停车）
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link ParkingEntity }>
     * @author 小粥
     * @since 2025/03/03
     */
    // 通过Id查询车位信息
    @RequestMapping(value = "/selectParkingById", method = RequestMethod.POST)
    public ParkingResponse<ParkingEntity> selectParkingById(@RequestBody Map<String, Object> map) {
        System.out.println(map.get("spotId").toString());
        int spotId = Integer.parseInt(map.get("spotId").toString());
        return parkingService.selectParkingById(spotId);
    }

    /**
     * 选择停车位
     *
     * @return {@link ParkingResponse }<{@link List }<{@link ParkingEntity }>>
     * @author 小粥
     * @since 2025/03/03
     */
    // 查询所有车位
    @RequestMapping(value = "/selectParking", method = RequestMethod.POST)
    public ParkingResponse<List<ParkingEntity>> selectParking() {
        return parkingService.selectParking();
    }

    /**
     * 选择按状态停车
     *
     * @return {@link ParkingResponse }<{@link List }<{@link ParkingEntity }>>
     * @author 小粥
     * @since 2025/03/03
     */
    // 查询可用车位
    @RequestMapping(value = "/selectParkingByStatus", method = RequestMethod.POST)
    public ParkingResponse<List<ParkingEntity>> selectParkingByStatus() {
        return parkingService.selectParkingByStatus();
    }

    /**
     * 创建 ReserCreate ReserCreate Resers
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    /* 预约车位 */
    @RequestMapping(value = "/createReser", method = RequestMethod.POST)
    public ParkingResponse<Integer> createReser(@RequestBody Map<String, Object> map) {
        ReserEntity reserEntity = new ReserEntity();
        reserEntity.setSpotId(Integer.parseInt(map.get("spotId").toString()));
        reserEntity.setUserId(Integer.parseInt(map.get("userId").toString()));
        reserEntity.setStartTime(LocalDateTime.parse(map.get("startTime").toString()));
        reserEntity.setEndTime(LocalDateTime.parse(map.get("endTime").toString()));
        reserEntity.setStatus(ReserStatus_active); // 设置初始状态为进行中
        System.out.println("map = " + map);
        return parkingService.createReser(reserEntity);
    }

    /**
     * 取消 Reser
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    /* 取消预约车位 */
    @RequestMapping(value = "/cancelReser", method = RequestMethod.POST)
    public ParkingResponse<Integer> cancelReser(@RequestBody Map<String, Object> map) {
        int reservationsId = Integer.parseInt(map.get("reservationId").toString());
        int spotId = Integer.parseInt(map.get("spotId").toString());
        return parkingService.cancelReser(reservationsId, spotId);
    }

    /**
     * 按 User ID 选择 reser
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link List }<{@link ReserEntity }>>
     * @author 小粥
     * @since 2025/03/03
     */
    /* 使用记录查询，通过用户Id */
    @RequestMapping(value = "/selectReserByUserId", method = RequestMethod.POST)
    public ParkingResponse<List<ReserEntity>> selectReserByUserId(@RequestBody Map<String, Object> map) {
        int userId = Integer.parseInt(map.get("userId").toString());
        return parkingService.selectReserByUserId(userId);
    }

    /**
     * 按景点 ID 更新停车
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    /* 修改车位信息管理员 */
    @RequestMapping(value = "/updateParkingBySpotIdAdmin", method = RequestMethod.POST)
    public ParkingResponse<Integer> updateParkingBySpotId(@RequestBody Map<String, Object> map) {
        ParkingEntity parkingEntity = new ParkingEntity();
        parkingEntity.setSpotId(Integer.parseInt(map.get("spotId").toString()));
        parkingEntity.setSpotNumber(map.get("spotNumber").toString());
        parkingEntity.setStatus(ParkingStatus_available); // 设置状态为可用
        parkingEntity.setLocation(map.get("location").toString());
        System.out.println("map = " + map);
        System.out.println("parkingEntity = " + parkingEntity);
        return parkingService.updateParkingBySpotIdAdmin(parkingEntity);
    }

    /**
     * 按站点 ID 删除停车位
     *
     * @param map 地图
     * @return {@link ParkingResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    /* 逻辑删除车位 */
    @RequestMapping(value = "/deleteParkingBySpotId", method = RequestMethod.POST)
    public ParkingResponse<Integer> deleteParkingBySpotId(@RequestBody Map<String, Object> map) {
        int spotId = Integer.parseInt(map.get("spotId").toString());
        return parkingService.deleteParkingBySpotId(spotId);
    }
}